package com.xj.openapi4j.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import  com.xj.openapi4j.exception.OpenAPIException;
import com.xj.openapi4j.service.token.YsToken;
import  com.xj.openapi4j.util.HttpUtil;
import com.xj.openapi4j.util.RequestTool;
import com.xj.util.Constance;
import com.xj.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Slf4j
@Service
public class DispatchService {
  @Autowired
  YsToken ysToken;
  @Autowired
  StockService stockService;
  @Autowired
	RedisTemplate redisTemplate;

	public void   transStockBydispatch() throws Exception {
		String token=ysToken.getToken();
		String requestUrl="https://c3.yonyoucloud.com/iuap-api-gateway/yonbip/sd/voucherdelivery/detail?access_token="+token;
		Set<String> sets = redisTemplate.boundSetOps(Constance.ysDispatch).members();
		Set<String> backsets=new HashSet<>(sets);
		Iterator<String> its= backsets.iterator();
		while(its.hasNext()){
			String dispatchVouchid=its.next();
			String str= HttpUtil.get(requestUrl+"&id="+dispatchVouchid);
			log.info("dispatchVouchid={}",dispatchVouchid);
			JSONObject response=JSONObject.parseObject(str);
			if("200".equals(response.getString("code"))){
				JSONObject data=response.getJSONObject("data");
				JSONArray jsonArray=data.getJSONArray("deliveryDetails");
				if(!CollectionUtils.isEmpty(jsonArray)){
					log.info("dispatchVoucher rows={}",jsonArray.size());
					JSONArray stockArr=new JSONArray();
					for(int i=0;i<jsonArray.size();i++){
						JSONObject detail=jsonArray.getJSONObject(i);
						String prodcode=detail.getString("productCode");
						JSONArray u8stock=	 stockService.getU8tock(prodcode);
						log.info("u8stock={}", JSON.toJSONString(u8stock));
						stockArr.addAll(u8stock);
					}
					log.info("u8 stock size={}",stockArr.size());
					stockService.transCurrentStock(stockArr);
				}
			}
	     	redisTemplate.boundSetOps(Constance.ysDispatch).remove(dispatchVouchid);
		}
	}



	public void   transStockBySaleout() throws Exception {
		String token=ysToken.getToken();
		String requestUrl="https://c3.yonyoucloud.com/iuap-api-gateway/yonbip/scm/salesout/detail?access_token="+token;
		Set<Long> sets = redisTemplate.boundSetOps(Constance.ysSaleout).members();
		Set<Long> backsets=new HashSet<>(sets);
		Iterator<Long> its= backsets.iterator();
		while(its.hasNext()){
			Long saleoutid=its.next();
			String str= HttpUtil.get(requestUrl+"&id="+saleoutid);
			log.info("saleout id={}",saleoutid);
			JSONObject response=JSONObject.parseObject(str);
			if("200".equals(response.getString("code"))){
				JSONObject data=response.getJSONObject("data");
				JSONArray jsonArray=data.getJSONArray("details");
				if(!CollectionUtils.isEmpty(jsonArray)){
					log.info("dispatchVoucher rows={}",jsonArray.size());
					JSONArray stockArr=new JSONArray();
					for(int i=0;i<jsonArray.size();i++){
						JSONObject detail=jsonArray.getJSONObject(i);
						String prodcode=detail.getString("product_cCode");
						JSONArray u8stock=	 stockService.getU8tock(prodcode);
						log.info("u8stock={}", JSON.toJSONString(u8stock));
						stockArr.addAll(u8stock);
					}
					log.info("u8 stock size={}",stockArr.size());
					stockService.transCurrentStock(stockArr);
				}
			}
			redisTemplate.boundSetOps(Constance.ysSaleout).remove(saleoutid);
		}
	}
}
